package com.tarim.streetdance.api;

import com.alibaba.fastjson.JSONObject;
import com.tarim.streetdance.model.CommentSecond;
import com.tarim.streetdance.model.CommentStair;
import com.tarim.streetdance.model.User;
import com.tarim.streetdance.service.CommentSecondService;
import com.tarim.streetdance.service.CommentStairService;
import com.tarim.streetdance.service.UserService;
import com.tarim.streetdance.util.Constants;
import com.tarim.streetdance.util.DateFormatUtils;
import com.tarim.streetdance.util.JsonResult;
import com.tarim.streetdance.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

@RestController
@CrossOrigin
@RequestMapping("/api/comment")
public class CommentApi {
    @Autowired
    private CommentStairService commentStairService;

    @Autowired
    private CommentSecondService commentSecondService;

    @Autowired
    private UserService userService;

    @Autowired
    private RedisUtil redisUtil;

    /**
     * 新增一级评论
     * @param commentStair
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/insertCommentStair")
    public JsonResult insertCommentStair (CommentStair commentStair, HttpServletRequest request, HttpServletResponse response) {
        JsonResult jsonResult;
        try {
            Object object = redisUtil.get(request.getHeader("token"));
            if (object == null) {
                jsonResult = new JsonResult(Constants.STATUS_FAIL, "登录后可发表评论");
                return jsonResult;
            }
            JSONObject jsonObject = (JSONObject) object;
            User user = jsonObject.toJavaObject(User.class);
            if (user.getDisableSend() == 1) {
                if (user.getEnableSendTime().getTime() > new Date().getTime()){
                    jsonResult = new JsonResult(Constants.STATUS_FAIL, "用户已被禁言，解禁时间："+DateFormatUtils.format(user.getRelieveTime(), DateFormatUtils.DEFAULT_TIME_FORMAT));
                    return jsonResult;
                }else {
                    User newUser = new User();
                    newUser.setDisableSend(0);
                    newUser.setId(user.getId());
                    userService.updateByPrimaryKeySelective(newUser);
                }
            }
            commentStair.setUserId(user.getId());
            commentStair.setUserCode(user.getUserCode());
            commentStair.setUserName(user.getUserName());
            int i = commentStairService.insertSelective(commentStair);
            if (i != 0) {
                jsonResult = new JsonResult(Constants.STATUS_SUCCESS, "评论成功");
            }else {
                jsonResult = new JsonResult(Constants.STATUS_FAIL, "评论失败");
            }
        }catch (Exception e) {
            e.printStackTrace();
            jsonResult = new JsonResult(Constants.STATUS_ERROR, "系统异常,请联系管理员");
        }
        return jsonResult;
    }

    /**
     * 新增二级评论
     * @param commentSecond
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/insertCommentSecond")
    public JsonResult insertCommentSecond (CommentSecond commentSecond, HttpServletRequest request, HttpServletResponse response) {
        JsonResult jsonResult;
        try {
            Object object = redisUtil.get(request.getHeader("token"));
            if (object == null) {
                jsonResult = new JsonResult(Constants.STATUS_FAIL, "登录后可发表评论");
                return jsonResult;
            }
            JSONObject jsonObject = (JSONObject) object;
            User user = jsonObject.toJavaObject(User.class);
            if (user.getDisableSend() == 1) {
                if (user.getEnableSendTime().getTime() > new Date().getTime()){
                    jsonResult = new JsonResult(Constants.STATUS_FAIL, "用户已被禁言，解禁时间："+DateFormatUtils.format(user.getRelieveTime(), DateFormatUtils.DEFAULT_TIME_FORMAT));
                    return jsonResult;
                }else {
                    User newUser = new User();
                    newUser.setDisableSend(0);
                    newUser.setId(user.getId());
                    userService.updateByPrimaryKeySelective(newUser);
                }
            }
            commentSecond.setUserId(user.getId());
            commentSecond.setUserCode(user.getUserCode());
            commentSecond.setUserName(user.getUserName());
            int i = commentSecondService.insertSelective(commentSecond);
            if (i != 0) {
                jsonResult = new JsonResult(Constants.STATUS_SUCCESS, "评论成功");
            }else {
                jsonResult = new JsonResult(Constants.STATUS_FAIL, "评论失败");
            }
        }catch (Exception e) {
            e.printStackTrace();
            jsonResult = new JsonResult(Constants.STATUS_ERROR, "系统异常,请联系管理员");
        }
        return jsonResult;
    }

    /**
     * 删除一级评论
     * @param commentStair
     * @return
     */
    @RequestMapping("/deleteCommentStair")
    public JsonResult deleteCommentStair (CommentStair commentStair) {
        JsonResult jsonResult;
        try {
            int i = commentStairService.updateForDelete(commentStair);
            if (i != 0) {
                jsonResult = new JsonResult(Constants.STATUS_SUCCESS, "删除成功");
            }else {
                jsonResult = new JsonResult(Constants.STATUS_FAIL, "删除失败");
            }
        }catch (Exception e) {
            e.printStackTrace();
            jsonResult = new JsonResult(Constants.STATUS_ERROR, "系统异常,请联系管理员");
        }
        return jsonResult;
    }

    /**
     * 删除二级评论
     * @param commentSecond
     * @return
     */
    @RequestMapping("/deleteCommentSecond")
    public JsonResult deleteCommentSecond (CommentSecond commentSecond) {
        JsonResult jsonResult;
        try {
            int i = commentSecondService.updateForDelete(commentSecond);
            if (i != 0) {
                jsonResult = new JsonResult(Constants.STATUS_SUCCESS, "删除成功");
            }else {
                jsonResult = new JsonResult(Constants.STATUS_FAIL, "删除失败");
            }
        }catch (Exception e) {
            e.printStackTrace();
            jsonResult = new JsonResult(Constants.STATUS_ERROR, "系统异常,请联系管理员");
        }
        return jsonResult;
    }
}
